package cn.dlc.test;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @author 邓立川
 * @date 2021/1/9 16:06
 * @desc 消息消费测试类
 */
public class ConsumerServerDemo {
    public static void main(String[] args) throws Exception{
        // 创建消费者，指定组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("my_consumer_group");
        // 设置服务器地址
        consumer.setNamesrvAddr("192.168.31.200:9876");
        // 指定主题和标签，*表示所有的标签
        consumer.subscribe("producer-topic","*");
        // 接收消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                System.out.println("消息是：" + list);
                // 返回消费成功的状态
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        consumer.start();
        System.out.println("消费者启动...");
        // 否则直接运行结束了
        Thread.sleep(Long.MAX_VALUE);
    }
}
